package Test1;

import java.util.Objects;


//Cloneable
//没有任何的抽象方法，那么说明它是一个标记性接口
//Cloneable表示一旦实现，那么当前类的对象就可以被克隆
//如果没有实现，当前类的对象就不能被克隆
public class Student implements Cloneable{
    String name;
    int age;
    int[] data;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return name + "," + age;
    }

    //重写之后的方法比较的为对象的属性值
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {

        //先把克隆对象中的数组获取出来
        int[] data = this.data;
        //创建新的数组
        int[] newData = new int[data.length];
        //拷贝数组中的数据
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        //调用父类中的方法克隆对象
        Student s = (Student) super.clone();
        //替换克隆出来对象的数组地址值
        s.data = newData;

        return s;
    }
}
